package com.yingjie.yingjiekitchen.controller;

import com.yingjie.commonbase.commons.ReturnObject;
import com.yingjie.commonbase.model.Kitchen;
import com.yingjie.commonbase.model.User;
import com.yingjie.commonsecurity.AuthUserContext;
import com.yingjie.yingjiekitchen.service.KitchenService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/kitchen")
public class KitchenController {

    @Resource
    private KitchenService kitchenService;
    @PostMapping("/create")
    public ReturnObject create(@RequestBody Kitchen kitchen){
        Integer userId = AuthUserContext.get().getUserId();
        Kitchen mykitchen = kitchenService.getMyKitchenInfo(userId);
        if(mykitchen!=null){
            return ReturnObject.ERROR("只能创建一个厨房");
        }

        Kitchen kit = new Kitchen();
        kit.setCreatedAt(new Date());
        kit.setKitchenDes(kitchen.getKitchenDes());
        kit.setKitchenName(kitchen.getKitchenName());
        kit.setKitchenAvatar(kitchen.getKitchenAvatar());
        kit.setKitchenUserId(userId);
        System.out.println(kit);
        Integer result = kitchenService.create(kit);
        if(result==0){
            return ReturnObject.ERROR("创建失败");
        }
        return  ReturnObject.SUCCESS(kit.getKitchenId());
    }

    @PostMapping("/getMyKitchenInfo")
    public ReturnObject myKitchenInfo(@RequestBody Kitchen kit){
        Kitchen kitchen = null;
        //优先获取的情侣厨房
        Integer couplesKitchenId = kit.getKitchenId();
        if(couplesKitchenId!=null){
            kitchen = kitchenService.getKitchenInfo(couplesKitchenId);
            return ReturnObject.SUCCESS(kitchen);
        }
        //获取用户自己的厨房
        Integer userId = AuthUserContext.get().getUserId();
        kitchen = kitchenService.getMyKitchenInfo(userId);
        return  ReturnObject.SUCCESS(kitchen);
    }

    //查看指定用户是否有厨房 并返回厨房信息
    @PostMapping("/getUserKitchenInfo")
    public ReturnObject getUserKitchenInfo(@RequestBody User user){
        Integer userId = user.getUserId();
        Kitchen kitchen = kitchenService.getMyKitchenInfo(userId);

        return  ReturnObject.SUCCESS(kitchen);
    }

}
